const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 20,
    total_page: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.inn_id){
      this.setData({
        inn_id: options.inn_id
      })
    }

    if (options.q) {
      var inn_id = options.q;
      var inn_id = unescape(decodeURI(inn_id));
      var inn_id = inn_id.substring(inn_id.lastIndexOf("=") + 1);
      this.setData({
        inn_id: inn_id
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getProductLIst();
    this.setData({
      url: app.globalData.base_url + 'images/shore_blank.png'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
    // 获取商品列表
  getProductLIst: function () {
    var that = this;
    var page = this.data.page;
    var inn_id = this.data.inn_id;
    var limit = this.data.limit;
    var params = {
      limit: that.data.limit,
      inn_id: inn_id,
      page: page
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'inn/getGoodsList',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.search;
          var totalPage = paging.total_page;
          var resultList = res.data.data.goods_list;
          var inn_info = res.data.data.inn_info
          if (page == 1) {
            var itemData = resultList;
          } else {
            var itemData = that.data.product.concat(resultList)
          }

          that.setData({
            total_page: totalPage,
            product: itemData,
            inn_info: inn_info
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  //查看商品详情
  productDetails: function (e) {
    var sku_code = e.currentTarget.dataset.sku_code;
    var inn_id = this.data.inn_id;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    wx.navigateTo({
      url: '../storeProductDetail/storeProductDetail?sku_code=' + sku_code + '&inn_id=' + inn_id + '&hidden=true'
    })
  },
  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      page: this.data.page + 1
    });
    this.getProductLIst();
  },
})